class BillingAccount < ActiveRecord::Base
  # belongs_to :billing_order  

# create new account
  attr_accessor :card_number, :card_verification, :first_name, :last_name

  # before_create :create_credit_card
  
  # # before_validation :credit_card
  # validate :credit_card_update, :on => :update, :if => :update_action?
  
  after_create :credit_card_store
  # validate :credit_card_store
  # before_update :credit_card_update
  
  before_destroy :credit_card_delete
  
  
  # Validations -----------------------------------------------------
  validates_presence_of   :user_id, :card_number, :card_verification, 
                          :first_name, :last_name, :expires_at_month, 
                          :expires_at_year, :on => :create
  # validates_numericality_of :card_number, :card_verification, 
  #                             :expires_at_year, :expires_at_month
  
  validates_uniqueness_of :user_id
  # -----------------------------------------------------------------
  
  #----- Act as FINITE STATE MACHINE -------------
    acts_as_state_machine :initial => :inactive, :column => :status

    # !!!!   for other special functions that
    # requires to change the status
    # use callback
    # state :inactive, :column => :status, :enter => :method1, .... read documentations

    state :inactive, :column => :status
    state :active, :column => :status
    state :canceled, :column => :status

    event :activate do
       transitions :from => :inactive, :to => :active
    end

    # event :deactivate do
    #   transitions :from => :active, :to => :inactive
    # end
    
    event :cancel do
      transitions :from => :active, :to => :canceled
    end

    # reference of actions
    def self.action_statuses
      {"active" => "deactivate", "inactive" => "activate"}
    end
  #-----------------------------------------------
  
  
  # validate all params mandatory
  def credit_card
    @credit_card = CreditCardV.new({:main => {
         :number     => card_number,
         :month      => expires_at_month,
         :year       => expires_at_year,
         :first_name => first_name,
         :last_name  => last_name,
         :verification_value  => card_verification,
         :type       => card_type},
         :add_options => {:user_id => user_id}}
    )
  end
  
  def credit_card_store
    credit_card
    validate_credit_card
    begin
      @credit_card.store_credit_card
      update_attributes({ :customer_profile_id => @credit_card.out_options[:customer_profile_id],
                          :customer_payment_id => @credit_card.out_options[:customer_payment_profile_id],
                          :updated_at => Date.today #what date today or now
                          #, status ?  state machine
                        })
      self.activate!
                      
    # rescue ZepFrog::CustomError => e
    #   errors.add_to_base e.inspect
    end
  end
  
  def validate_credit_card
    unless @credit_card.valid?
     @credit_card.errors.full_messages.each do |message|
       errors.add_to_base message
     end
     raise ZepFrog::CustomError, (self.class.to_s + "." + "validate_credit_card" + ":" + errors.full_messages.to_s)
    end    
  end
  
  def credit_card_update
    errors.add_to_base "UPDATE NOT included to this version"
    raise ZepFrog::CustomError, (self.class.to_s + "." + "credit_card_update" + ":" + (errors.full_messages.to_s || "Do not allow update now!!!"))
  end
  
  def credit_card_delete
    if customer_profile_id
    CreditCardV.delete_card({:customer_profile_id => customer_profile_id})
    end
  end
  # 
  # # validate :create_credit_card
  # def create_credit_card
  #   credit_card
  #   validate_credit_card
  #   begin
  #     i = CreditCard.credit_card_store({:user_id => (user_id.to_s + "__" + last_name), :zipcode => "07306", :credit_card => @credit_card})
  #     # customer_profile = CreditCard.customer_profile({:user_id => (user_id.to_s + "__" + last_name), :zipcode => "07306", :credit_card => @credit_card})
  #     puts i[:customer_profile_id].to_s + "i[:customer_profile_id]"
  #      self.customer_profile_id = i[:customer_profile_id]
  #      self.customer_payment_profile_id = i[:customer_payment_profile_id]
  #     # puts customer_profile.authorization.to_s + "id !!"
  #     # customer_payment_profile_id = CreditCard.customer_profile({})
  #   # rescue StandardError => e
  #   #   errors.add_to_base e.inspect
  #   #
  #        end
  # end
  # 
  # def validate_credit_card
  #      unless @credit_card.valid?
  #        @credit_card.errors.full_messages.each do |message|
  #          errors.add_to_base message
  #        end
  #      end    
  #    end
  #    
  # private
  # 
  # # def credit_card
  # #     @credit_card = CreditCard.new({
  # #          :number     => card_number,
  # #          :month      => expires_at_month,
  # #          :year       => expires_at_year,
  # #          :first_name => first_name,
  # #          :last_name  => last_name,
  # #          :verification_value  => card_verification,
  # #          :type       => card_type}
  # #        )
  # #        # return @credit_card
  # #        
  # #  end
end
